function Queue() {
    var items = []
    // 优先队列
    function QueueEle (ele, priority) {
        this.ele = ele
        this.priority = priority
    }
    this.enquue = function (ele) {
        items.push(ele)
    }
    // this.enquue = function (ele, priority) {
    //     var queueEle = new QueueEle(ele, priority)
    //     if (this.isEmpty()) {
    //         items.push(queueEle)
    //     } else {
    //         var adder = false
    //         for (var i = 0; i < items.length; i++) {
    //             if (queueEle.priority < items[i].priority) {
    //                 items.splice(i, 0, queueEle)
    //                 adder = true
    //                 break
    //             }
    //         }
    //         if (!adder) {
    //             items.push(queueEle)
    //         }
    //     }
    // }
    this.dequeue = function (ele) {
        return items.shift(ele)
    }
    this.front = function(ele) {
        return items[0]
    }
    this.isEmpty = function(ele) {
        return items.length === 0
    }
    this.size = function(ele) {
        return items.length
    }
    this.all = function(ele) {
        return items
    }
}

// var pQueue = new Queue()
// pQueue.enquue("abc", 10)
// pQueue.enquue("cba", 5)
// pQueue.enquue("nba", 12)
// pQueue.enquue("mba", 3)
// pQueue.enquue('gh', 1)

// 遍历所有的元素
// var size = pQueue.size()
// for (var i = 0; i < size; i++) {
//     var item = pQueue.dequeue()
//     console.log(item.ele + "-" + item.priority)
// }
// var queue = new Queue()
// queue.enquue('a')
// queue.enquue('b')
// console.log(items);
// console.log(queue.dequeue());
// console.log(queue.front());
// console.log(queue.isEmpty());


// 击鼓传花

function play(peoples, nums) {
    let queue = new Queue()
    for (let i = 0; i < peoples.length; i++) {
        queue.enquue(peoples[i])
    }
    while(queue.size() > 1) {
        for (let i = 1; i < nums; i++) {
            queue.enquue(queue.dequeue())
        }
        console.log(queue.dequeue() + '被淘汰');
    }
    var lastName = queue.dequeue()
    console.log(`最后一人是${lastName}`);
}
var names = ['John','Jack','Camila','Ingrid','Carl'];
play(names,8)